<template>
	<z-container left-icon-size="0" title="订单列表" showNoLoginPage noLoginLabel="登录后可查看订单信息">
		<z-sticky >
			<uv-gap height="10"></uv-gap>
			<z-tabs scrollable :list="orderTabs" :current="state.current" @change="changeCurrent" :inactiveStyle="inactiveStyle" :activeStyle="activeStyle"></z-tabs>
			<uv-gap height="10"></uv-gap>
		</z-sticky>
		<view class="ss-p-x-32 ss-p-b-24">
			<OrderList :list="state.pagination.data" />
			<z-loadMore :status="state.loadStatus" :list="state.pagination.data" ></z-loadMore>
		</view>		
	</z-container>  
</template>

<script setup>
	import sheep from '@/sheep'
	import { activeStyle,inactiveStyle,orderTabs } from "@/sheep/enu/index.js"
	import applyFlow from "@/pages/components/applyFlow.vue"
	import OrderList from "@/pages/components/OrderList.vue"
	const userInfo = computed(() => sheep.$store('user').userInfo)
	const pagination = {
	  data: [],
	  current_page: 1,
	  total: 1,
	  last_page: 1,
	};
	const state = reactive({
		current:0,
		loadStatus:'loading',
		pagination: {
		  data: [],
		  current_page: 1,
		  total: 1,
		  last_page: 1,
		},
	})
	onLoad(() => {
		// getList()
	})
	onShow(() => {
		if(!state.pagination.data.length) onReach()
	})
	onMounted(() => {
		uni.$on('onReachOrder',() => {
			onReach()
		})
	})
	onUnmounted(() => {
		uni.$off('onReachOrder')
	})
	
	function changeCurrent(e){
		state.current = e.index
		onReach()
	}
	
	async function getList(page = 1, limit = 10){
		state.loadStatus = 'loading';
		let res = await sheep.$api.user.orderlist({
		  status:orderTabs[state.current]?.status,
		  limit,
		  page
		});
		uni.stopPullDownRefresh()
		if (res.code === 1) {
		  let list = state.pagination.data.concat(res.data.data);
		  state.pagination = {
		    ...res.data,
		    data: list,
		  };
			
		  if (state.pagination.current_page < state.pagination.last_page) {
		    state.loadStatus = 'loadmore';
		  } else {
		    state.loadStatus = 'nomore';
		  }
		}
	}
	// 加载更多
	function loadmore() {
	  if (state.loadStatus !== 'nomore') {
	    getList(state.pagination.current_page + 1);
	  }
	}
	
	function onReach(){
		  state.pagination = pagination;
		  getList();
		  setTimeout(function () {
		    uni.stopPullDownRefresh();
		  }, 800);
	}
	
	// 上拉加载更多
	onReachBottom(() => {
	  loadmore();
	});
	
	//下拉刷新
	onPullDownRefresh(() => {
	  state.pagination = pagination;
	  getList();
	  setTimeout(function () {
	    uni.stopPullDownRefresh();
	  }, 800);
	});
</script>

<style scoped lang="scss">
	
</style>
